const http = require('http')
const fs = require('fs')

const server = http.createServer()
server.on('request', (req, res) => {
  console.log('有人请求数据');
  console.log(req.url);
  switch (req.url) {
    case '/':
      fs.readFile('./sources/hello.txt', (err, data) => {
        if (!err) {
          res.setHeader('Content-Type', 'text/plain; charset=utf-8')
          res.end(data)
        }
      })
      break
    case '/index':
      fs.readFile('./sources/index.html', (err, data) => {
        if (!err) {
          res.setHeader('Content-Type', 'text/html; charset=utf-8')
          res.end(data)
        }
      })
      break
    case '/jpg':
      fs.readFile('./sources/index.jpg', (err, data) => {
        if (!err) {
          res.setHeader('Content-Type', 'image/jpeg')
          res.end(data)
        }
      })
      break
    default:
      res.end('请求路径出错')
      break
  }
})
server.listen(5000, () => {
  console.log('5000端口的服务器已启动。。。。');
})